#include "MaterialDialog.h"
#include "Editor.h"
#include "shlwapi.h"
#include "Basic/FileSystem.h"
#include "../Resource.h"

#pragma comment(lib, "shlwapi.lib")

namespace FinalRendering
{
	namespace Editor
	{
		using namespace GxRendering::UI;

		MaterialDialog::MaterialDialog()
		{
			SetClientWidth(640);
			SetClientHeight(500);
			SetText(L"Select Material");
			SetMinimizeBox(false);
			SetMaximizeBox(true);
			SetShowInTaskBar(false);
			
			OnShow.Bind(this, &MaterialDialog::form_Show);
			OnClose.Bind(this, &MaterialDialog::form_Close);
			Container * pnl = new Container();
			pnl->SetHeight(50);
			pnl->DockStyle = Control::dsBottom;
			pnl->BorderStyle = BS_NONE;
			ui->AddChild(pnl);

			Container * pnl2 = new Container();
			pnl2->SetWidth(170);
			pnl2->DockStyle = Control::dsRight;
			pnl2->BorderStyle = BS_NONE;
			pnl->AddChild(pnl2);
			
			Button * btn = new Button();
			btn->SetText(L"&OK");
			btn->Posit(0, 10, 70, 28);
			btn->OnClick.Bind(this, &MaterialDialog::btnOK_Clicked);
			pnl2->AddChild(btn);
			
			btn = new Button();
			btn->SetText(L"&Close");
			btn->Posit(80, 10, 70, 28);
			btn->OnClick.Bind(this, &MaterialDialog::btnCancel_Clicked);
			pnl2->AddChild(btn);


			list = new MaterialListView();
			list->DockStyle = Control::dsFill;
			ui->AddChild(list);

			Container * pnlTop = new Container();
			pnlTop->SetHeight(30);
			pnlTop->BorderStyle = BS_NONE;
			pnlTop->DockStyle = Control::dsTop;
			ui->AddChild(pnlTop);

			Label * lbl = new Label();
			lbl->SetText(L"Directory:");
			lbl->Posit(10,10,70,20);
			pnlTop->AddChild(lbl);

			lblCurPath = new Label();
			lblCurPath->Posit(80,10,20,20);
			pnlTop->AddChild(lblCurPath);

			pnl = new Container();
			pnl->SetWidth(50);
			pnl->BorderStyle = BS_NONE;
			pnl->DockStyle = Control::dsRight;
			pnlTop->AddChild(pnl);

			btnUp = new ToolButton(L"Up", ToolButton::bsNormal, &Bitmap(IDB_GOUP), &Color(0,0,0,0));
			btnUp->Posit(10, 2, 26, 26);
			btnUp->OnClick.Bind(this, &MaterialDialog::btnUp_Clicked);
			pnl->AddChild(btnUp);

			if (!FileExists(WorldEditor->GetMaterialPath()))
				CreateDirectory(WorldEditor->GetMaterialPath().GetStrPtr(), 0);
			list->SetDirectory(WorldEditor->GetMaterialPath());
			list->OnDirectoryChanged.Bind(this, &MaterialDialog::list_DirectoryChanged);
			list->OnDblClick.Bind(this, &MaterialDialog::list_DblClicked);
		}

		void MaterialDialog::list_DblClicked(UI_Base * sender)
		{
			if (list->SelectedIndex != -1)
			{
				if (list->items[list->SelectedIndex]->Type == MaterialListView::Item::itFile)
				{
					FileName = lblCurPath->GetText();
					MakeDirectory(FileName);
					FileName += list->items[list->SelectedIndex]->FileName;
					SetDialogResult(BaseForm::OK);
				}
				else
				{
					list->DoubleClicked(sender);
				}
			}

		}

		void MaterialDialog::list_DirectoryChanged(UI_Base * sender)
		{
			wchar_t path[1024];
			memset(path, 0, sizeof(wchar_t)*1024);
			PathRelativePathTo(path, WorldEditor->GetMaterialPath().GetStrPtr(), FILE_ATTRIBUTE_DIRECTORY, 
				list->GetCurrentDirectory().GetStrPtr(), FILE_ATTRIBUTE_DIRECTORY);
			lblCurPath->SetText(path);
			if (wcscmp(path,L".")==0)
			{
				btnUp->Enabled = false;
			}
			else
				btnUp->Enabled = true;
		}

		void MaterialDialog::btnUp_Clicked(UI_Base * sender)
		{
			list->SetDirectory(list->GetCurrentDirectory() + L"../");
		}

		void MaterialDialog::btnOK_Clicked(UI_Base * sender)
		{
			list_DblClicked(sender);
		}

		void MaterialDialog::btnCancel_Clicked(UI_Base * sender)
		{
			SetDialogResult(BaseForm::Cancel);
		}

		void MaterialDialog::form_Show(Object * sender, GxWinGUI::EventArgs e)
		{
			list->SetDirectory(list->GetCurrentDirectory());
			list->StartLoading();
		}

		void MaterialDialog::form_Close(Object * sender, GxWinGUI::WindowCloseEventArgs & e)
		{
			list->StopLoading();
		}
	}
}